/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Controladores.AppointmentJpaController;
import Controladores.MedicalrecordJpaController;
import Controladores.VeterinarianJpaController;
import Controladores.exceptions.PreexistingEntityException;
import entidades.Appointment;
import entidades.Medicalrecord;
import entidades.Veterinarian;
import java.awt.Dimension;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 *
 * @author Gatoper
 */
public class JPIAppointment extends javax.swing.JPanel {

    /**
     * Creates new form Appointment
     */
    public JPIAppointment() {
        initComponents();
        this.setSize(new Dimension(300, 400));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LID = new javax.swing.JLabel();
        LCharge = new javax.swing.JLabel();
        LDate = new javax.swing.JLabel();
        LNotes = new javax.swing.JLabel();
        Lmedicalrecord = new javax.swing.JLabel();
        Lveterinarian = new javax.swing.JLabel();
        CBmedicalrecord = new javax.swing.JComboBox();
        CBveterinarian = new javax.swing.JComboBox();
        TID = new javax.swing.JTextField();
        TCharge = new javax.swing.JTextField();
        TNotes = new javax.swing.JTextField();
        CBday = new javax.swing.JComboBox();
        CBmonth = new javax.swing.JComboBox();
        CByear = new javax.swing.JComboBox();
        Bcrear = new javax.swing.JButton();

        LID.setText("ID:");

        LCharge.setText("Charge:");

        LDate.setText("Date:");

        LNotes.setText("Notes:");

        Lmedicalrecord.setText("MedicalRrecord");

        Lveterinarian.setText("Veterinarian:");

        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties = new HashMap<>();
        emf = Persistence.createEntityManagerFactory("HPVAS");
        em = emf.createEntityManager();
        Medicalrecord medicalrecord=new Medicalrecord();
        MedicalrecordJpaController mjc=new MedicalrecordJpaController(emf);
        List l1=mjc.findMedicalrecordEntities();
        String [] aux1=new String[l1.size()];
        for(int i=0;i<l1.size();i++)
        aux1[i]=((Medicalrecord)l1.get(i)).getId()+"";
        CBmedicalrecord.setModel(new javax.swing.DefaultComboBoxModel(aux1));

        Veterinarian veterinarian=new Veterinarian();
        VeterinarianJpaController vjc=new VeterinarianJpaController(emf);
        List l=vjc.findVeterinarianEntities();
        String [] aux=new String[l.size()];
        for(int i=0;i<l.size();i++)
        aux[i]=((Veterinarian)l.get(i)).getName()+"";
        CBveterinarian.setModel(new javax.swing.DefaultComboBoxModel(aux));

        CBday.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12","13", "14", "15", "16" }));

        CBmonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12" }));

        CByear.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"1991", "1992", "1993", "1994","1995", "1996", "1997", "1998" }));

        Bcrear.setText("Create");
        Bcrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BcrearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(TID))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Lmedicalrecord)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CBmedicalrecord, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Lveterinarian)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CBveterinarian, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LCharge)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(TCharge))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(Bcrear))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LDate)
                            .addComponent(LNotes))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(CBday, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(TNotes))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LID)
                    .addComponent(TID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LCharge)
                    .addComponent(TCharge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TNotes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LNotes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lmedicalrecord)
                    .addComponent(CBmedicalrecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lveterinarian)
                    .addComponent(CBveterinarian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Bcrear)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BcrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BcrearActionPerformed
        // TODO add your handling code here:
        boolean flag=true;
        if(TCharge.getText().length()<1) flag=false;
        if(TID.getText().length()<1) flag=false;
        if(TNotes.getText().length()<1) flag=false;
        if(flag){
            EntityManagerFactory emf;
            EntityManager em;
            Map<String, String> properties = new HashMap<>();
            emf = Persistence.createEntityManagerFactory("HPVAS");
            em = emf.createEntityManager();
            Date date=new Date(Integer.valueOf(this.CByear.getSelectedItem().toString()),Integer.valueOf(this.CBmonth.getSelectedItem().toString()),Integer.valueOf(this.CBday.getSelectedItem().toString()));
            MedicalrecordJpaController mjc=new MedicalrecordJpaController(emf);
                Medicalrecord h=mjc.findMedicalrecord(Long.parseLong(this.CBmedicalrecord.getSelectedItem().toString()));
            VeterinarianJpaController vjc=new VeterinarianJpaController(emf);
                Veterinarian v=vjc.findVeterinarian(this.CBveterinarian.getSelectedItem().toString());
            Appointment a=new Appointment(date, Integer.parseInt(this.TCharge.getText()), this.TNotes.getText(), h, v, Long.parseLong(this.TID.getText()));
            AppointmentJpaController ajc=new AppointmentJpaController(emf);
            try {        
                ajc.create(a);
                JOptionPane.showMessageDialog(this, "Exito");
            } catch (PreexistingEntityException ex) {
                Logger.getLogger(JPIAppointment.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(JPIAppointment.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Por favor llene todos los campos");
        }
    }//GEN-LAST:event_BcrearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Bcrear;
    private javax.swing.JComboBox CBday;
    private javax.swing.JComboBox CBmedicalrecord;
    private javax.swing.JComboBox CBmonth;
    private javax.swing.JComboBox CBveterinarian;
    private javax.swing.JComboBox CByear;
    private javax.swing.JLabel LCharge;
    private javax.swing.JLabel LDate;
    private javax.swing.JLabel LID;
    private javax.swing.JLabel LNotes;
    private javax.swing.JLabel Lmedicalrecord;
    private javax.swing.JLabel Lveterinarian;
    private javax.swing.JTextField TCharge;
    private javax.swing.JTextField TID;
    private javax.swing.JTextField TNotes;
    // End of variables declaration//GEN-END:variables
}
